package com.example.echoewish.ui.main_activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.echoewish.R;
import com.example.echoewish.util.DataGenerator;
import com.example.echoewish.util.StatusBarUtils;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayout;

public class MainActivity extends AppCompatActivity {
    //Tab的个数 防止以后修改tab个数所以设为 定值
    private static final int TAB_NUM=3;
    private TabLayout eTabLayout;
    private Fragment []eFragments;
    private NavigationView navigationView;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        StatusBarUtils.setWindowStatusBarColor(this, R.color.normal);
        setContentView(R.layout.activity_main);
        //冲数据生成类中 获取生成的fragment
        eFragments= DataGenerator.getFragments();
        //并且初始化tab
        initView();
    }

    private void initView()
    {
        eTabLayout =findViewById(R.id.bottom_tab_layout);
        navigationView=findViewById(R.id.nav_main);
        navigationView.setItemIconTintList(null);
        //重载点击事件 图片 和文字颜色的变化 （注意如果不用新的setcustomeview 图片颜色会怪怪的）
        eTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //改变fragment
                  onTabItemSelected(tab.getPosition());
                  //改变图片 文字
                  for(int i=0;i<eTabLayout.getTabCount();i++)
                  {
                      View view=eTabLayout.getTabAt(i).getCustomView();
                      ImageView icon=view.findViewById(R.id.tab_content_image);
                      TextView title=view.findViewById(R.id.tab_content_text);
                      if(i==tab.getPosition())
                      {
                          icon.setImageResource(DataGenerator.eTabPressed[i]);
                          title.setTextColor(Color.parseColor("#8ec7ec"));
                      }
                       else
                      {
                          icon.setImageResource(DataGenerator.eTabNoPressed[i]);
                          title.setTextColor(Color.parseColor("#cecece"));
                      }
                  }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        //载入所有控件
        for(int i=0;i<TAB_NUM;++i)
        {
            eTabLayout.addTab(eTabLayout.newTab().setCustomView(R.layout.etab_layout));
            View view=eTabLayout.getTabAt(i).getCustomView();
            ImageView icon=view.findViewById(R.id.tab_content_image);
            icon.setImageResource(DataGenerator.eTabNoPressed[i]);
            TextView title=view.findViewById(R.id.tab_content_text);
            title.setText(DataGenerator.eTabTitle[i]);
        }

        //默认进入首页
        View view=eTabLayout.getTabAt(0).getCustomView();
        ImageView icon=view.findViewById(R.id.tab_content_image);
        icon.setImageResource(DataGenerator.eTabPressed[0]);
        getSupportFragmentManager().beginTransaction().replace(R.id.home_container,eFragments[0]).commit();
    }

    //改变fragment
    private void onTabItemSelected(int position)
    {
        Fragment fragment=null;
        switch (position)
        {
            case 0:
                fragment=eFragments[0];
                break;
            case 1:
                fragment=eFragments[1];
                break;
            case 2:
                fragment=eFragments[2];
                break;
        }
        if(fragment!=null)
            getSupportFragmentManager().beginTransaction().replace(R.id.home_container,fragment).commit();
    }
}